package com.itheima.d4_collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

/*
    目标：斗地主游戏的案例开发。
    业务需求分析:
        业务: 总共有54张牌。
        点数: "3","4","5","6","7","8","9","10","J","Q","K","A","2"
        花色: "♠", "♥", "♣", "♦"
        大小王: "👲" , "🃏"
        点数分别要组合4种花色，大小王各一张。
        斗地主：发出51张牌，剩下3张作为底牌。
 */
public class GameDemo {
    public static void main(String[] args) {
        //1.创建一个集合，用于存储54张牌。
        String[]numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] colors = {"♠","♥","♣","♦"};
        int size = 0;
        //2.遍历数组，将点数和花色组合，存储到集合中。
        ArrayList<Card> cards = new ArrayList<>();
        for (String number : numbers) {
            size++;
            for (String color : colors) {
                Card card = new Card(number, color, size);
                cards.add(card);
            }
        }
        Card card1 = new Card("", "🃏", ++size);
        Card card2 = new Card("", "👲", ++size);
        Collections.addAll(cards, card1, card2);
        System.out.println(cards);
        //2.洗牌。
        System.out.println("=====================================================");
        Collections.shuffle(cards);
        System.out.println(cards);
        System.out.println("=====================================================");
        //3.发牌。
        ArrayList<Card> player1 = new ArrayList<>();
        ArrayList<Card> player2 = new ArrayList<>();
        ArrayList<Card> player3 = new ArrayList<>();
        ArrayList<Card> dipai = new ArrayList<>();
        for (int i = 0; i < cards.size(); i++) {
            Card card = cards.get(i);
            if (i >= cards.size() - 3) {
                dipai.add(card);
            } else if (i % 3 == 0) {
                player1.add(card);
            } else if (i % 3 == 1) {
                player2.add(card);
            } else {
                player3.add(card);
            }
        }
        System.out.println("底牌：" + dipai);
        System.out.println("玩家1：" + player1);
        System.out.println("玩家2：" + player2);
        System.out.println("玩家3：" + player3);
        System.out.println("=====================================================");
        //4.排序。
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
        Collections.sort(dipai);
        System.out.println("玩家1：" + player1);
        System.out.println("玩家2：" + player2);
        System.out.println("玩家3：" + player3);
        System.out.println("底牌：" + dipai);
        System.out.println("=====================================================");
        // 随机选择一个玩家作为地主
        Random random = new Random();
        int landlordIndex = random.nextInt(3); // 生成0, 1, 2中的一个随机数

        ArrayList<Card> landlord;
        switch (landlordIndex) {
            case 0:
                landlord = player1;
                System.out.println("玩家1成为地主");
                break;
            case 1:
                landlord = player2;
                System.out.println("玩家2成为地主");
                break;
            default:
                landlord = player3;
                System.out.println("玩家3成为地主");
                break;
        }

        // 将底牌发给地主
        landlord.addAll(dipai);
        Collections.sort(landlord);

        System.out.println("玩家1：" + player1);
        System.out.println("玩家2：" + player2);
        System.out.println("玩家3：" + player3);


    }
}










